package com.huishu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.Result;
import com.huishu.user.pojo.model.MenuDO;
import com.huishu.user.pojo.model.RoleMenuDO;
import com.huishu.user.service.MenuService;
import com.huishu.user.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单表前端控制器
 *
* @author generator@ljj
* @since 2023-04-21
*/
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    @PostMapping("/add")
    public Result add(@RequestBody MenuDO menuDO) {
        return menuService.add(menuDO);
    }

    @PostMapping("/update")
    public Result update(@RequestBody MenuDO menuDO) {
        return menuService.modify(menuDO);
    }

    @GetMapping("/findAll")
    public Result finAll() {
        return menuService.findAll();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        menuService.removeById(id);
        // 删除菜单角色关系
        LambdaQueryWrapper<RoleMenuDO> queryWrapper = Wrappers.lambdaQuery(RoleMenuDO.class)
                .eq(RoleMenuDO::getMenuId,id);
        roleMenuService.remove(queryWrapper);
        return Result.success();
    }

}
